package cn.com.kh;

import cn.com.elite.EliteBean;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

public class KHHSTUtil {
    private static final Log log = LogFactory.getLog(KHHSTUtil.class);
    private static final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    private static final PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
    private static final PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

    /**
     * @param eliteBean: 数据对象
     * @param phoneNo:   国际号码
     * @return String 剔除前缀后的号码
     * @author LiJT
     * @description 通过国际号码获取实际号码
     * @date 2023-05-22 14:50
     */
    public static String getPhoneNo(EliteBean eliteBean, String phoneNo) {

        log.debug("[国际号码处理]-原始完整号码:" + phoneNo);
        String rePhoneNo = getPhoneNo(phoneNo);
        return rePhoneNo;
    }

    /**
     * @param phoneNo: 国际号码
     * @return String 剔除前缀后的号码
     * @author LiJT
     * @description 通过国际号码获取实际号码
     * @date 2023-05-22 14:38
     */
    public static String getPhoneNo(String phoneNo) {

        String rePhoneNo = "";

        if (!KHUtil.isNull(phoneNo)) {
            try {
                Phonenumber.PhoneNumber chinaNumberProto = phoneUtil.parse(phoneNo, "CN");
                rePhoneNo = String.valueOf(chinaNumberProto.getNationalNumber());
                log.debug("[国际号码处理]-剔除后手机号:" + rePhoneNo);

            } catch (Exception e) {
                log.error("[国际号码处理]-剔除操作异常:" + e.getMessage(), e);
            }
        }
        rePhoneNo = KHUtil.isNull(rePhoneNo) ? phoneNo : rePhoneNo;
        return rePhoneNo;
    }

    /**
     * @param phoneNo: 国际号码
     * @return JSONObject 解析后国际号码信息
     * @author LiJT
     * @description 通过国际号码获取号码综合信息(国际区号, 国际号码, 运营商, 归属地)
     * @date 2023-05-22 14:38
     */
    public static JSONObject getPhoneNoJson(String phoneNo) {

        JSONObject rePhoneNo = new JSONObject();

        if (!KHUtil.isNull(phoneNo)) {
            try {
                Phonenumber.PhoneNumber chinaNumberProto = phoneUtil.parse(phoneNo, "CH");
                rePhoneNo.put("CountryCode", chinaNumberProto.getCountryCode());
                rePhoneNo.put("NationalNumber", chinaNumberProto.getNationalNumber());
                rePhoneNo.put("NameForNumber", carrierMapper.getNameForNumber(chinaNumberProto, Locale.ENGLISH));
                rePhoneNo.put("DescriptionForNumber", geocoder.getDescriptionForNumber(chinaNumberProto, Locale.CHINA));
                log.debug("[国际号码处理]-剔除后手机号:" + rePhoneNo.toString());

            } catch (Exception e) {
                log.error("[国际号码处理]-剔除操作异常:" + e.getMessage(), e);
            }
        }

        return rePhoneNo;
    }

}
